﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation.CApAIVCLogManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation.Resources;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Resources;


namespace DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation;

internal class CApAIVCLogManager
{
  private static string m_filePath;
  private int m_numErrors;
  private DateTime m_beginTime;
  private DateTime m_endTime;
  private List<KeyValuePair<string, TimeSpan>> m_times = new List<KeyValuePair<string, TimeSpan>>();
  private List<string> m_simulationsNotEnded = new List<string>();
  private static string DIVIDER = string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_DIVIDER"));

  internal event CApAIVCLogErrorEventHandler LogError;

  public static string FilePath => CApAIVCLogManager.m_filePath;

  public int NumErrors => this.m_numErrors;

  public CApAIVCLogManager()
  {
    string str = "AutomaticInterferencevolumecreation.log";
    CApAIVCLogManager.m_filePath = Path.GetTempPath() + str;
  }

  public void Init()
  {
    this.m_numErrors = 0;
    this.m_times.Clear();
    string[] contents = new string[2]
    {
      string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_NAME")),
      CApAIVCLogManager.DIVIDER
    };
    File.WriteAllLines(CApAIVCLogManager.m_filePath, contents);
  }

  public void BeginTime(string step)
  {
    this.m_beginTime = DateTime.Now;
    string str = string.Format(CApAIVCLogManager.GetResourceManager().GetString(step)) + "...";
    using (StreamWriter streamWriter = new StreamWriter(CApAIVCLogManager.m_filePath, true))
      streamWriter.WriteLine(str);
  }

  public void EndTime(string step)
  {
    this.m_endTime = DateTime.Now;
    this.m_times.Add(new KeyValuePair<string, TimeSpan>(step, this.m_endTime - this.m_beginTime));
    this.WriteOneLine(CApAIVCLogManager.DIVIDER);
  }

  public void WriteStatusMessageToLog(object sender, CApAIVCLogMessageEventArgs args)
  {
    switch (args.LogMessage.Code)
    {
      case eApAIVCLogCode.SIMULATION_ERROR:
        this.WriteSimulationErrors(args.LogMessage.Object, args.LogMessage.Errors);
        break;
      case eApAIVCLogCode.SIMULATION_NOT_ENDED:
        this.m_simulationsNotEnded.Add(args.LogMessage.Object);
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_CREATION_BEGIN:
      case eApAIVCLogCode.SWEPT_VOLUME_CREATION_SUCCEEDED:
      case eApAIVCLogCode.SWEPT_VOLUME_DELETION:
      case eApAIVCLogCode.INTERFERENCE_VOLUME_CREATION_SUCCEEDED:
        this.WriteCreationSuccess(args.LogMessage.Code, args.LogMessage.Object);
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_EMPTY:
      case eApAIVCLogCode.SWEPT_VOLUME_CREATION_FAILED:
      case eApAIVCLogCode.INTERFERENCE_VOLUME_CREATION_FAILED:
        this.WriteCreationFail(args.LogMessage.Code, args.LogMessage.Object);
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_COLLISION:
      case eApAIVCLogCode.SWEPT_VOLUME_SAME_ROBOT:
      case eApAIVCLogCode.SWEPT_VOLUME_DELETION_SKIP:
      case eApAIVCLogCode.INTERFERENCE_ZONE:
        this.WriteStepStatus(args.LogMessage.Code, args.LogMessage.NumObjects);
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_DELETION_NO_VOLUMES:
      case eApAIVCLogCode.INTERFERENCE_VOLUME_NO_VOLUMES:
        this.WriteNoVolumes();
        break;
    }
  }

  public void WriteTimeToLog()
  {
    if (this.m_times.Count <= 0)
      return;
    using (StreamWriter streamWriter = new StreamWriter(CApAIVCLogManager.m_filePath, true))
    {
      streamWriter.WriteLine();
      streamWriter.WriteLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_TIME_SUMMARY")));
      foreach (KeyValuePair<string, TimeSpan> time in this.m_times)
      {
        string str1 = string.Format(CApAIVCLogManager.GetResourceManager().GetString(time.Key)) + ":\t";
        TimeSpan timeSpan1 = time.Value;
        TimeSpan timeSpan2 = new TimeSpan(timeSpan1.Days, timeSpan1.Hours, timeSpan1.Minutes, timeSpan1.Seconds);
        string str2 = str1 + timeSpan2.ToString();
        streamWriter.WriteLine(str2);
      }
    }
  }

  public void WriteStopMessageToLog()
  {
    using (StreamWriter streamWriter = new StreamWriter(CApAIVCLogManager.m_filePath, true))
    {
      streamWriter.WriteLine(CApAIVCLogManager.DIVIDER);
      streamWriter.WriteLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_STOP")));
    }
  }

  public void WriteNonEndedSimulationToLog()
  {
    if (this.m_simulationsNotEnded.Count <= 0)
      return;
    using (StreamWriter streamWriter = new StreamWriter(CApAIVCLogManager.m_filePath, true))
    {
      streamWriter.WriteLine();
      streamWriter.WriteLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SIMULATION_NOT_ENDED")));
      foreach (string str in this.m_simulationsNotEnded)
        streamWriter.WriteLine(str);
    }
  }

  public static void OpenFile() => Process.Start(CApAIVCLogManager.m_filePath);

  private static ResourceManager GetResourceManager() => AIVCResources.ResourceManager;

  private void WriteOneLine(string message)
  {
    using (StreamWriter streamWriter = new StreamWriter(CApAIVCLogManager.m_filePath, true))
      streamWriter.WriteLine(message);
  }

  private void WriteSimulationErrors(string objectName, ArrayList errors)
  {
    ArrayList arrayList = this.FilterErrorMessages(errors);
    if (arrayList.Count == 0)
      return;
    string str = string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SIMULATION"), (object) objectName);
    using (StreamWriter streamWriter = new StreamWriter(CApAIVCLogManager.m_filePath, true))
    {
      streamWriter.WriteLine(str);
      for (int index = 0; index < arrayList.Count; ++index)
        streamWriter.WriteLine($"{(index + 1).ToString()}) {arrayList[index]?.ToString()}");
    }
    this.m_numErrors += arrayList.Count;
    this.FireLogErrorEvent();
  }

  private ArrayList FilterErrorMessages(ArrayList errors)
  {
    ArrayList arrayList = new ArrayList();
    foreach (string error in errors)
    {
      if (error.Contains("[Error]"))
        arrayList.Add((object) error);
    }
    return arrayList;
  }

  private void WriteCreationFail(eApAIVCLogCode code, string objectName)
  {
    switch (code)
    {
      case eApAIVCLogCode.SWEPT_VOLUME_EMPTY:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_EMPTY_SV"), (object) objectName));
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_CREATION_FAILED:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SV_FAIL"), (object) objectName));
        break;
      case eApAIVCLogCode.INTERFERENCE_VOLUME_CREATION_FAILED:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_IV_FAIL"), (object) objectName));
        break;
    }
    ++this.m_numErrors;
    this.FireLogErrorEvent();
  }

  private void WriteCreationSuccess(eApAIVCLogCode code, string objectName)
  {
    switch (code)
    {
      case eApAIVCLogCode.SWEPT_VOLUME_CREATION_BEGIN:
        this.WriteOneLine("");
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SV_BEGIN"), (object) objectName));
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_CREATION_SUCCEEDED:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SV_SUCCEED"), (object) objectName));
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_DELETION:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SV_DELETION"), (object) objectName));
        break;
      case eApAIVCLogCode.INTERFERENCE_VOLUME_CREATION_SUCCEEDED:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_IV_SUCCEED"), (object) objectName));
        break;
    }
  }

  private void WriteStepStatus(eApAIVCLogCode code, int numObjects)
  {
    switch (code)
    {
      case eApAIVCLogCode.SWEPT_VOLUME_COLLISION:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SV_COLLISION"), (object) numObjects));
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_SAME_ROBOT:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SV_SAME_ROBOT")));
        break;
      case eApAIVCLogCode.SWEPT_VOLUME_DELETION_SKIP:
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SV_DELETION_SKIP")));
        break;
      case eApAIVCLogCode.INTERFERENCE_ZONE:
        if (numObjects == 0)
        {
          this.WriteNoVolumes();
          break;
        }
        this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SYNCHRONIZING_LOCATIONS")));
        break;
    }
  }

  private void WriteNoVolumes()
  {
    this.WriteOneLine(string.Format(CApAIVCLogManager.GetResourceManager().GetString("AIVC_LOG_SKIP")));
  }

  private void FireLogErrorEvent()
  {
    if (this.LogError == null)
      return;
    this.LogError((object) this, new CApAIVCLogErrorEventArgs(this.m_numErrors));
  }
}
